/*
 * location.h
 *
 *  Created on: 2012-05-21
 *      Author: Aco
 */

#ifndef LOCATION_H_
#define LOCATION_H_

#include "node.h"

/*
 * Location - geografksa lokacija, stablo vezanih listi koje cine
 * kompletnu hijerarhiju mijesta i regiona
 */
typedef struct _locationS
{
	struct _locationS * parent;
	struct _locationS * firstChild;
	struct _locationS * next;

	CNode *node[8];		// Nodovi prijavljeni na ovu lokaciju
	int numNodes;
	int level;
	char name[64];
	int endPoint;

	int kelvins;
	int timestamp;		// Broj tickova kada je upisana temperatura
} CLocation;

CLocation * cl_createRoot();
CLocation * cl_createLocation(CLocation * root, const char * addr);
CLocation * cl_getByAddr(CLocation * root, const char * addr);
int cl_countLevels(const char * addr);
void cl_dumpTree(int id, CLocation * root);

void cl_traverseTree(CLocation * root, void (*func) (CLocation*,void*), void * param);
int cl_countNodes(CLocation * loc);
int cl_countChildren(CLocation * loc);
void cl_constructFullAddress(CLocation * loc, char * text);

int cl_attachNode(CLocation * loc, CNode * node, int max);
int cl_detachNode(CLocation * loc, CNode * node);
CNode * cl_findNode(CLocation * loc, unsigned int ipAddr, int port);
CNode * cl_recFindNode(CLocation * loc, unsigned int ipAddr, int port);
CNode * cl_upwardFindNode(CLocation * loc, unsigned int ipAddr, int port);
CNode * cl_bidirFindNode(CLocation * loc, unsigned int ipAddr, int port);
void cl_reverseAddress(const char * src, char * dst);

int cl_isAncestorOf(CLocation * parent, CLocation * child);
int cl_isSiblingOf(CLocation * l1, CLocation * l2);
CLocation * cl_getTopLevel(CNode * n, CLocation * loc);

#endif /* LOCATION_H_ */
